package com.hzncc.scaffold.controller.Front.OnePlusX;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.OnePlusxExamDetailBo;
import com.hzncc.scaffold.entity.Bo.TrainingDetailBo;
import com.hzncc.scaffold.entity.Other.*;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.OneplusxexamMapper;
import com.hzncc.scaffold.mapper.OneplusxtrainingMapper;
import com.hzncc.scaffold.service.*;
import com.hzncc.scaffold.utils.NumberUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-06 15:23
 * @Description: null
 */
@Api(tags = "前台接口-1+X管理")
@RequestMapping("/onePlusX")
@RestController
public class OnePlusXController {

    @Autowired
    OneplusxtrainingService oneplusxtrainingService;

    @Autowired
    OneplusxexamService oneplusxexamService;

    @Autowired
    OneplusxtrainingMapper oneplusxtrainingMapper;

    @Autowired
    OneplusxexamMapper oneplusxexamMapper;

    @Autowired
    InvigilatorconfirmService invigilatorconfirmService;

    @Autowired
    ExamineeapplyService examineeapplyService;

    @Autowired
    TrainerapplyService trainerapplyService;

    @GetMapping("/trainingList")
    @ApiOperation("1+X培训列表")
    public ResultT trainingList(Integer page,Integer pageSize,Integer type,Integer pageNum) {
        Integer pageData = NumberUtils.pageNum(page, pageNum);
        if(type != 4){
            LambdaQueryWrapper<Oneplusxtraining> lqw = new LambdaQueryWrapper<>();
            lqw.eq(type != null, Oneplusxtraining::getType, type);
            lqw.orderByDesc(Oneplusxtraining::getStayAtTop);
            lqw.orderByDesc(Oneplusxtraining::getSortTime);

            if (pageData != null && pageSize != null && pageData > 0 && pageSize > 0) {
                PageHelper.startPage(pageData, pageSize);
            }

            List<Oneplusxtraining> oneplusxtrainingList = oneplusxtrainingService.list(lqw);
            HashMap<String,Object> map = new HashMap<>();
            map.put("rows",oneplusxtrainingList);
            map.put("count",new PageInfo<>(oneplusxtrainingList).getTotal());
            return ResultT.success(map);
//            return ResultT.success(oneplusxtrainingList);
        }else {

            LambdaQueryWrapper<Oneplusxexam> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Oneplusxexam::getStatus,1);

            if (pageData != null && pageSize != null && pageData > 0 && pageSize > 0) {
                PageHelper.startPage(pageData, pageSize);
            }

            List<Oneplusxexam> oneplusxexamList = oneplusxexamService.list(lqw);
            List<OneplusExamListVo> oneplusExamListVos = BeanUtil.copyToList(oneplusxexamList, OneplusExamListVo.class);

            for (OneplusExamListVo oneplusExamListVo : oneplusExamListVos) {
                oneplusExamListVo.setIsExam(true);
            }
//            PageInfo<OneplusExamListVo> info = new PageInfo<>();
//            info.setList(oneplusExamListVos);
//            info.setTotal(new PageInfo<>(oneplusxexamList).getTotal());
            HashMap<String,Object> map = new HashMap<>();
            map.put("rows",oneplusExamListVos);
            map.put("count",new PageInfo<>(oneplusxexamList).getTotal());
            return ResultT.success(map);
        }

    }

        @GetMapping("/trainingDetail")
    @ApiOperation("1+X培训详情")
    public ResultT trainingDetail(String activityId){

        QueryWrapper<TrainingDetailBo> qw = new QueryWrapper<>();
        qw.eq("oneplusxtraining.id",activityId);
        TrainingDetailBo trainingDetailBo = oneplusxtrainingMapper.trainingDetail(qw);

        TrainingDetailVo trainingDetailVo = BeanUtil.copyProperties(trainingDetailBo, TrainingDetailVo.class, "attachFile");

        if(StrUtil.isNotEmpty(trainingDetailBo.getAttachFile())){
            JSONArray jsonArray = new JSONArray(trainingDetailBo.getAttachFile());
            List<AttachFileVo> attachFileVoList = JSONUtil.toList(jsonArray, AttachFileVo.class);
            trainingDetailVo.setAttachFile(attachFileVoList);
        }
        return ResultT.success(trainingDetailVo);
    }

    @GetMapping("/examDetail")
    @ApiOperation("学生考试详情")
    public ResultT examDetail(String activityId){
        QueryWrapper<OnePlusxExamDetailBo> qw = new QueryWrapper<>();
        qw.eq("oneplusxexam.id",activityId);
        OnePlusxExamDetailBo onePlusxExamDetailBo = oneplusxexamMapper.myOnePlusxExam(qw);
        OnePlusxExamDetailVo onePlusxExamDetailVo = new OnePlusxExamDetailVo();

        onePlusxExamDetailVo.setId(onePlusxExamDetailBo.getId());
        onePlusxExamDetailVo.setActivityEnd(onePlusxExamDetailBo.getActivityEnd());
        onePlusxExamDetailVo.setActivityStart(onePlusxExamDetailBo.getActivityStart());
        onePlusxExamDetailVo.setCreateTime(onePlusxExamDetailBo.getCreateTime());
        onePlusxExamDetailVo.setDevice(onePlusxExamDetailBo.getDevice());
        onePlusxExamDetailVo.setDeviceType(onePlusxExamDetailBo.getDeviceType());
        onePlusxExamDetailVo.setEndTime(onePlusxExamDetailBo.getEndTime());
        onePlusxExamDetailVo.setExamType(onePlusxExamDetailBo.getExamType());
        onePlusxExamDetailVo.setExamineeAmount(onePlusxExamDetailBo.getExamineeAmount());
        onePlusxExamDetailVo.setInfo(onePlusxExamDetailBo.getInfo());
        onePlusxExamDetailVo.setStartTime(onePlusxExamDetailBo.getStartTime());
        onePlusxExamDetailVo.setTitle(onePlusxExamDetailBo.getTitle());


        LambdaQueryWrapper<Invigilatorconfirm> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Invigilatorconfirm::getExamId,activityId);
        List<Invigilatorconfirm> invigilatorconfirms = invigilatorconfirmService.list(lqw);

        List<InvigilatorVo> invigilatorVos = BeanUtil.copyToList(invigilatorconfirms, InvigilatorVo.class);

        onePlusxExamDetailVo.setInvigilatorConfirms(invigilatorVos);

        ProfessionVo professionVo = new ProfessionVo();
        professionVo.setId(onePlusxExamDetailBo.getProfessionId());
        professionVo.setName(onePlusxExamDetailBo.getProfessionName());

        onePlusxExamDetailVo.setProfession(professionVo);

        SkillVo skillVo = new SkillVo();
        skillVo.setId(onePlusxExamDetailBo.getSkillId());
        skillVo.setName(onePlusxExamDetailBo.getSkillName());

        onePlusxExamDetailVo.setSkill(skillVo);

        return ResultT.success(onePlusxExamDetailVo);
    }

    @PostMapping("/examApply")
    @ApiOperation("学生考试-我要报名")
    public ResultT examApply(ActivityIdVo activityIdVo){

        String activityId = activityIdVo.getActivityId();

        if(!StpUtil.isLogin()){
            return ResultT.fail("您未登录");
        }

        LambdaQueryWrapper<Examineeapply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Examineeapply::getUserId,StpUtil.getLoginIdAsInt());
        lqw.eq(Examineeapply::getExamId,activityIdVo);

        List<Examineeapply> examineeapplyList = examineeapplyService.list(lqw);

        if(CollUtil.isNotEmpty(examineeapplyList)){
            Examineeapply examineeapply = examineeapplyList.get(0);
            if(examineeapply.getFirstCheck()==1&&examineeapply.getFinalCheck()==0){
                return ResultT.fail("申请正在审核中，请勿重复提交");
            }
            if(examineeapply.getFirstCheck()==1&&examineeapply.getFinalCheck()==2){
                return ResultT.fail("申请已被拒绝，请核验您的资料");
            }
            if(examineeapply.getFirstCheck()==1&&examineeapply.getFinalCheck()==1){
                return ResultT.fail("您已报名成功，请勿重复提交");
            }
        }

        Examineeapply examineeapply = new Examineeapply();
        examineeapply.setUserId(StpUtil.getLoginIdAsInt());
        examineeapply.setExamId(activityId);
        examineeapply.setCreateTime(DateUtil.now());


        boolean save = examineeapplyService.save(examineeapply);

        if(save){
            return ResultT.success("报名成功");
        }else {
            return ResultT.fail("报名失败");
        }
    }

    @PostMapping("/trainingApply")
    @ApiOperation("考务培训-我要报名")
    public ResultT trainingApply(@RequestBody TrainingApplyVo trainingApplyVo){

        String activityId = trainingApplyVo.getActivityId();
        List<FilesVo> attachFile = trainingApplyVo.getAttachFile();

        JSONArray jsonArray = JSONUtil.parseArray(attachFile);
        String attachFileJson = JSONUtil.toJsonStr(jsonArray);

        if(!StpUtil.isLogin()){
            return ResultT.fail("用户未登录");
        }


        LambdaQueryWrapper<Trainerapply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Trainerapply::getUserId,StpUtil.getLoginIdAsInt());
        lqw.eq(Trainerapply::getTrainingId,activityId);

        List<Trainerapply> trainerapplyList = trainerapplyService.list(lqw);
        if(CollUtil.isNotEmpty(trainerapplyList)){
            Trainerapply trainerapply = trainerapplyList.get(0);
            if(trainerapply.getStatus()==0){
                return ResultT.fail("申请正在审核中，请勿重复提交");
            }
            if(trainerapply.getStatus()==2){
                return ResultT.fail("申请已被拒绝，请核验您的资料");
            }
            if(trainerapply.getStatus()==1){
                return  ResultT.fail("您已报名成功，请勿重复提交");
            }
        }

        Trainerapply trainerapply = new Trainerapply();
        trainerapply.setUserId(StpUtil.getLoginIdAsInt());
        trainerapply.setTrainingId(activityId);
        trainerapply.setCreateTime(DateUtil.now());
        trainerapply.setAttachFile(attachFileJson);

        boolean save = trainerapplyService.save(trainerapply);
        if(save){
            return ResultT.success("报名成功");
        }else {
            return ResultT.fail("报名失败");
        }
    }
}
